package org.lanqiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lanqiao.admins.Administrator;
import org.lanqiao.mapper.NoticeMapper;
import org.lanqiao.notice.INoticeService;
import org.lanqiao.notices.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<Notice> noticeList() {
        return noticeMapper.selectList(null);
    }

    @Override
    public List<Notice> noticeListByCondition(Map<String, Object> queryMap) {
        return noticeMapper.selectList(queryWrapper(queryMap));
    }

    @Override
    public IPage<Notice> noticePage(Integer pagenum, Integer pagesize) {
        IPage<Notice> page = new Page<>(pagenum,pagesize);
        page = noticeMapper.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<Notice> noticePageByCondition(Integer pagenum, Integer pagesize, Map<String, Object> queryMap) {
        IPage<Notice> page = new Page<>(pagenum,pagesize);
        page = noticeMapper.selectPage(page,queryWrapper(queryMap));
        return page;
    }

    @Override
    public Integer addNotice(Notice notice) {
        if(notice == null){
            return 0;
        }else {
            try {
               noticeMapper.insert(notice);
                return  1;
            }catch (Exception e){
                e.printStackTrace();
                return -1;
            }
        }
    }

    @Override
    public Integer updateNotice(Notice notice) {
        if(notice == null){
            return 0;
        }else {
            try {
                noticeMapper.updateById(notice);
                return  1;
            }catch (Exception e){
                e.printStackTrace();
                return -1;
            }
        }
    }

    @Override
    public Integer deleteNoticeById(Integer id) {
        try {
            noticeMapper.deleteById(id);
            return  1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    private QueryWrapper<Notice> queryWrapper(Map<String,Object> queryMap){
        // 查询条件
        QueryWrapper<Notice> queryWrapper = null;
        if(queryMap !=null){
            queryWrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(queryMap.get("title"))){
                queryWrapper.like("title",queryMap.get("title"));
            }
            /*
            if(!StringUtils.isEmpty(queryMap.get("starttime")) && !StringUtils.isEmpty(queryMap.get("endtime")) ){
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:");

                    //Date startDatetime = dateFormat.parse((String)queryMap.get("stattime"));
                   // Date endDatetime = dateFormat.parse((String)queryMap.get("endtime"));
                   String startDatetime = (String)queryMap.get("starttime");
                   String endDatetime = (String)queryMap.get("endtime");
                   queryWrapper.between("updatetime",startDatetime,endDatetime);

            }*/
            if(!StringUtils.isEmpty(queryMap.get("starttime"))){

                String startDatetime = (String)queryMap.get("starttime");
                queryWrapper.ge("updatetime",startDatetime);

            }
            if(!StringUtils.isEmpty(queryMap.get("endtime")) ){


                String endDatetime = (String)queryMap.get("endtime");
                queryWrapper.le("updatetime",endDatetime);

            }
        }
        return  queryWrapper;
    }



}
